package com.shanghaionstar.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.widget.DatePicker;

import com.onstar.cn.common.jsBridge.BridgeWebView;
import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.common.util.DeviceUtil;
import com.onstar.cn.common.util.MyPost;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.map.util.ToastUtil;
import com.shanghaionstar.OnstarApplication;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.MainTabActivity;
import com.shanghaionstar.activity.WithActionSheetHelper;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Created by 7828wc on 2016/6/29.
 */
public class H5JSObject {
    private Context activity;
    private BridgeWebView webView;

    public H5JSObject(Context activity) {
        this.activity = activity;
    }

    public void setWebView(BridgeWebView webView) {
        this.webView = webView;
    }


    //@JavascriptInterface
//    public void showSource(String share) {
//        if (share != null) {
//            if (activity instanceof CommonWebViewActivity) {
//                ((CommonWebViewActivity) activity).showShare(true, share);
//            }
//        }
//    }

//    @JavascriptInterface
//    public String getToken() {
//        String token = ApplicationUtil.getInstance().getToken();
//        return token;
//    }

    @JavascriptInterface
    public void openCalendar(String dateString) {
        final SimpleDateFormat sdf = new SimpleDateFormat(FinalConstants.DATE_FORMAT);
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        try {
            Date date = sdf.parse(dateString);
            calendar.setTime(date);
        } catch (Exception e) {
            e.printStackTrace();
            calendar.setTime(new Date(System.currentTimeMillis()));
        }
        DatePickerDialog datePickerDialog = new DatePickerDialog(activity, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int montyOfYear, int dayOfMonth) {
                StringBuilder sb = new StringBuilder();
                sb.append(year);
                sb.append("-");
                sb.append(montyOfYear + 1);
                sb.append("-");
                sb.append(dayOfMonth);

                try {
                    final String result = sdf.format(sdf.parse(sb.toString()));
                    if (webView != null) {
                        MyPost.post(new Runnable() {
                            @Override
                            public void run() {
                                H5Entity h5Entity = new H5Entity();
                                h5Entity.setDate(result);
//                                String url = "javascript:resultDate(" + JsonUtils.object2Json(h5Entity) + ")";
//                                webView.loadUrl(url);
                                webView.callHandler("resultDate", GsonHelper.toJson(h5Entity), null);
                            }
                        });
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    LogManager.getInstance().e(e);
                }
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.show();
    }

    @JavascriptInterface
    public void call(String phone) {
        LogManager.getInstance().d("phone:" + phone);
        AlertDialog.Builder builder = new AlertDialog.Builder(this.activity);
        builder.setPositiveButton(R.string.call_out, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
                        + activity.getString(R.string.buick_connect_phone)));
                activity.startActivity(callIntent);
            }
        });
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.setTitle(activity.getString(R.string.dialing));
        builder.setMessage(activity.getString(R.string.buick_connect_phone));
        builder.create().show();
    }

    @JavascriptInterface
    public void openCamera() {
        WithActionSheetHelper helper = new WithActionSheetHelper((BaseActivity) activity);
        helper.showDialogToTakeChoosePicture();
    }

    /**
     * 必须添加@JavascriptInterface标签，HTML的JS才会有效
     */
    @JavascriptInterface
    public void finish() {
        if (activity instanceof Activity) {
            ((Activity) activity).finish();
        }
    }

    @JavascriptInterface
    public void dial(final String phone) {
        LogManager.getInstance().d("phone:" + phone);
        AlertDialog.Builder builder = new AlertDialog.Builder(this.activity);

        builder.setPositiveButton(R.string.call_out, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phone));
                activity.startActivity(callIntent);
            }
        });
        builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        builder.setTitle(activity.getString(R.string.dialing));
        builder.setMessage(phone);
        builder.create().show();
    }


    /**
     * h5消息列表点击按钮跳转到首页“遥控”tab
     */
    @JavascriptInterface
    public void goRemotePage() {
        Log.e("goRemotePage", "跳转到遥控Tab");
        Intent intent = new Intent();
        intent.setClass(this.activity, MainTabActivity.class);
        intent.putExtra("from", "remote");
        activity.startActivity(intent);

    }

    /**
     * h5消息列表点击按钮跳转到首页“遥控”tab
     */
    @JavascriptInterface
    public String getOnstarVersion() {
        Log.e("goRemotePage", "跳转到遥控Tab");
        return new DeviceUtil(OnstarApplication.getApplication()).getAppVersion();

    }

    public class H5Entity {
        private String date;

        public String getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date = date;
        }
    }
}
